package zhangtao.bwie.com.yuekao2;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import Bean.ShopDataBean;

/**
 * Created by ZhangTAO on 2017/12/21.
 */

public class MyAdapters extends RecyclerView.Adapter<MyAdapters.ViewHolder>{
    private Context context;
    private List<ShopDataBean.DataBean> data;
    public MyAdapters(ShopData shopData, List<ShopDataBean.DataBean> data) {
        this.context = shopData;
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v1 = View.inflate(context, R.layout.item, null);
        ViewHolder vh1 = new ViewHolder(v1);
        return vh1;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        String images = data.get(position).getImages();
        if(images.contains("|")) {
            String[] split = images.split("[|]");
            holder.icons.setImageURI(split[0]);
        }else {
            holder.icons.setImageURI(images);
        }
        holder.price.setText(data.get(position).getPrice()+"");
        holder.desc.setText(data.get(position).getTitle());
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                monit.setOnItem(view,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        if(data == null) {
            return 0;
        }
        return data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private SimpleDraweeView icons;
        private TextView price;
        private TextView desc;
        private View root;
        public ViewHolder(View itemView) {
            super(itemView);
            root = itemView;
            icons = itemView.findViewById(R.id.shop_icons);
            price = itemView.findViewById(R.id.shop_price);
            desc = itemView.findViewById(R.id.shop_desc);
        }
    }
    private onitemclick monit;
    public void setOnItemCheck(onitemclick click) {
        this.monit = click;
    }
    public interface onitemclick {
        void setOnItem(View v,int pos);
    }
}
